package com.marvels.dubbo.coreservice.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dragon.loan.service.RerunQuotaDragonService;
import com.fintech.common.vo.FintechResult;
import com.fintech.third.model.interfacedata.EdcDataCallOperationRecordsDto;
import com.fintech.third.model.shzx.EdcZxPersonalLoanOrderInfo;
import com.fintech.third.service.interfacedata.DataCallOperationRecordsService;
import com.fintech.third.service.shzx.ShangHaiLogInService;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.utils.MarvelsLogUtil;
import com.marvels.dubbo.coreservice.dto.DecisionMsgDTO;
import com.marvels.dubbo.coreservice.service.MarvelDecisionService;


@Service
public class MarvelDecisionServiceImpl implements MarvelDecisionService {
	
	@Autowired
    private RerunQuotaDragonService rerunQuotaDragonService;
	
	
	@Autowired
	private DataCallOperationRecordsService dataCallOperationRecordsService;
	
	@Autowired
	private ShangHaiLogInService shangHaiLogInService;
	
	/**获取决策试算核定金额
	 * @author 娄凯琦
	 * @date 2018年9月21日  
	 * @param decisionMsgDTO
	 * @return
	 * @throws MarvelsBusinessException 
	 */
	@Override
	public DecisionMsgDTO trailApprovedAmount(DecisionMsgDTO decisionMsgDTO) throws MarvelsBusinessException {
		if(decisionMsgDTO==null){
			throw new MarvelsBusinessException("服务参数为空");
		}
		MarvelsLogUtil.getInstance().info("DecisionServiceImpl class method TrailApprovedAmount para==="+decisionMsgDTO);
		String loanId = decisionMsgDTO.getLoanId();
		if(loanId==null||"".equals(loanId.trim())){
			throw new MarvelsBusinessException("贷款参数为空");
		}
		FintechResult fintechResult  = rerunQuotaDragonService.rerunQuotaAmount(loanId);//调用核心核定金额试算借口
		MarvelsLogUtil.getInstance().info("DecisionServiceImpl class method TrailApprovedAmount fintechResult ===="+fintechResult);
		if(fintechResult == null){
			throw new MarvelsBusinessException("调用决策试算核定金额接口失败");
		}
		BigDecimal trailAmount = new BigDecimal((fintechResult==null||fintechResult.getData()==null)?"0":fintechResult.getData().toString());
		MarvelsLogUtil.getInstance().info("DecisionServiceImpl class method TrailApprovedAmount trailAmount ===="+trailAmount);
		decisionMsgDTO.setTrailAmount(trailAmount);
		return decisionMsgDTO;
	}

	/**查询51公积金
	 * @author 娄凯琦
	 * @date 2018年9月21日  
	 * @param decisionMsgDTO
	 * @return
	 */
	@Override
	public DecisionMsgDTO search51gjj(DecisionMsgDTO decisionMsgDTO) throws MarvelsBusinessException{
		if(decisionMsgDTO==null){
			throw new MarvelsBusinessException("服务参数为空");
		}
		MarvelsLogUtil.getInstance().info("DecisionServiceImpl class method search51gjj para==="+decisionMsgDTO);
		String loanId = decisionMsgDTO.getLoanId();
		if(loanId==null||"".equals(loanId.trim())){
			throw new MarvelsBusinessException("贷款参数为空");
		}
		Map<String, String> dataMap=new HashMap<>();
		dataMap.put("loanId",loanId);
		List<Map<String, Object>> listgjj=null;//gjj51DataService.listgjj(dataMap);
		decisionMsgDTO.setListgjj(listgjj);
		return decisionMsgDTO;
	}

	
	
	/**获取上海资信贷款余额
	 * @author 娄凯琦
	 * @date 2018年9月21日  
	 * @param decisionMsgDTO
	 * @return
	 * @throws MarvelsBusinessException
	 */
	@Override
	public DecisionMsgDTO getShangHaiAmount(DecisionMsgDTO decisionMsgDTO) throws MarvelsBusinessException {
		if(decisionMsgDTO==null){
			throw new MarvelsBusinessException("服务参数为空");
		}
		MarvelsLogUtil.getInstance().info("DecisionServiceImpl class method getShangHaiAmount start para==="+decisionMsgDTO);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		// 基本客户信息
		paramMap.put("custIdCard", decisionMsgDTO.getCustIdCard());
		paramMap.put("interfaceCode", "SHANGHAIZX001");
		paramMap.put("productType", decisionMsgDTO.getProducType());
		EdcDataCallOperationRecordsDto edcDataCallOperationRecordsInfo = dataCallOperationRecordsService
				.edcDataCallOperationRecordsInfo(paramMap);//查询最新的loanId
		String newLoanId = edcDataCallOperationRecordsInfo.getLoanId();
		EdcZxPersonalLoanOrderInfo orderInfo = shangHaiLogInService.queryPersonalLoanOrderInfoByLoanId(newLoanId);
		BigDecimal shAllBalance = orderInfo.getLoanRemainder();//上海资信贷款余额
		decisionMsgDTO.setShAllBalance(shAllBalance);
		MarvelsLogUtil.getInstance().info("DecisionServiceImpl class method getShangHaiAmount end==="+decisionMsgDTO);
		return decisionMsgDTO;
	}

}
